/*
 * Group 3, User Interface Programming II (vt11)
 * Pooya | Flora | Monica | Can
 */
package vical.view;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import vical.controller.PreferenceController;
import vical.data.DataManager;

/**
 * The parent frame for all windows in this application
 * Deal with some features for all children, like record size and location
 * 
 * @author Can
 */
public abstract class SmartFrame extends JFrame {

    public SmartFrame() {
        initComponents();
    }

    private void initComponents() {
        // actionlisteners
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                saveParameters();
            }
        });
        this.setSize(WIDTH, HEIGHT);
        // load preferences
        this.setSize(PreferenceController.getSize(this.getClass().getName(), getDefaultSize()));
        this.setLocation(PreferenceController.getLocation(this.getClass().getName(), getDefaultLocation()));
        this.setExtendedState(PreferenceController.getExtendedState(this.getClass().getName(), NORMAL));
    }
    
    protected abstract Dimension getDefaultSize();
    protected abstract Point getDefaultLocation();
    
    /**
     * Same the necessary parameters between session
     * Multiply instance of the same class will share the same parameters
     */
    public void saveParameters() {
        // TODO: Pooya, it should be saved after each modification, not in the end!
        DataManager.getInstance().save();

        if ((this.getExtendedState() & Frame.MAXIMIZED_BOTH) != Frame.MAXIMIZED_BOTH) {
            PreferenceController.setSize(this.getClass().getName(), this.getSize());
            PreferenceController.setLocation(this.getClass().getName(), this.getLocation());
        }
        PreferenceController.setExtendedState(this.getClass().getName(), this.getExtendedState());
    }
}
